package com.motorola.accessories.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteFullException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class AccessoriesProvider extends ContentProvider {

    private static final String TAG = "AccessoriesProvider";
    private SQLiteOpenHelper mDbHelper;
    private static final int ALL_INFO = 1;
    private static final UriMatcher URI_MATCHER = new UriMatcher(
            UriMatcher.NO_MATCH);
    static {
        URI_MATCHER.addURI(ProviderInfo.AUTHORITY, ProviderInfo.TABLE_NAME, ALL_INFO);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        if (db == null || !db.isOpen()) {
            Log.w(TAG, "delete:Null DB returned from DBHelper for a writable/readable database.");
            return 0;
        }
        String tableName = null;
        int count = 0;
        final int match = URI_MATCHER.match(uri);
        switch (match) {
        case ALL_INFO:
            tableName = ProviderInfo.TABLE_NAME;
            break;
        default:
            throw (new IllegalArgumentException(TAG + "Invalid URL: " + uri));
        }
        try {
            db.acquireReference();
            db.beginTransaction();
            count = db.delete(tableName, selection, selectionArgs);
            db.setTransactionSuccessful();
        } catch (SQLiteFullException fullEx) {
            Log.w(TAG, fullEx);
        } catch (Exception e) {
            Log.w(TAG, e);
        } finally {
            try {
                db.endTransaction();
            } catch (SQLiteFullException fullEx) {
                Log.w(TAG, fullEx);
            } catch (Exception e) {
                Log.w(TAG, e);
            }
            db.releaseReference();
        }
        return count;
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        if (db == null || !db.isOpen()) {
            Log.w(TAG, "insert:Null DB returned from DBHelper for a writable/readable database.");
            return null;
        }
        long rowId = -1;
        String tableName = null;
        String nullColHack = null;
        int match = URI_MATCHER.match(uri);
        switch (match) {
        case ALL_INFO:
            tableName = ProviderInfo.TABLE_NAME;
            break;
        default:
            throw (new IllegalArgumentException(TAG + "Invalid URL: " + uri));
        }
        try {
            db.acquireReference();
            db.beginTransaction();
            rowId = db.insert(tableName, nullColHack, values);
            db.setTransactionSuccessful();
        } catch (SQLiteFullException fullEx) {
            Log.w(TAG, fullEx);
        } catch (Exception e) {
            Log.w(TAG, e);
        } finally {
            try {
                db.endTransaction();
            } catch (SQLiteFullException fullEx) {
                Log.w(TAG, fullEx);
            } catch (Exception e) {
                Log.w(TAG, e);
            }
            db.releaseReference();
        }
        return Uri.withAppendedPath(uri, Long.toString(rowId));
    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        mDbHelper = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = mDbHelper.getReadableDatabase();
        if (db == null || !db.isOpen()) {
            Log.w(TAG, "query:Null DB returned from DBHelper for a writable/readable database.");
            return null;
        }
        String groupBy = null;
        final int match = URI_MATCHER.match(uri);
        final SQLiteQueryBuilder queryB = new SQLiteQueryBuilder();
        switch (match) {
            case ALL_INFO:
                queryB.setTables(ProviderInfo.TABLE_NAME);
                break;
            default:
                throw (new IllegalArgumentException(TAG + "URL: " + uri
                        + " WHERE: " + selection));
        }
        Cursor cursor = null;
        try {
            db.acquireReference();
            cursor = queryB.query(db, projection, selection,
                selectionArgs, groupBy, null, sortOrder);
            return cursor;
        } catch (SQLiteException exception) {
            Log.e(TAG, "NCR!");
        } finally {
            db.releaseReference();
        }
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }


}
